package com.tensuqare.user.controller;

import com.tensuqare.user.pojo.User;
import com.tensuqare.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private HttpServletRequest request;

    //发送短信息
    @PostMapping("sendsms/{mobile}")
    public Result sendMessage(@PathVariable String mobile) {
        userService.sendMessage(mobile);
        return new Result(true, StatusCode.OK, "发送成功");
    }

    //注册用户
    @PostMapping("register/{code}")
    public Result register(@PathVariable String code, @RequestBody User user) {
        userService.register(user, code);
        return new Result(true, StatusCode.OK, "注册成功");
    }

    //增加用户
    @PostMapping()
    public Result addUser(@RequestBody User user) {
        try {
            userService.addUser(user);
            return new Result(true, StatusCode.OK, "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "新增失败！");
        }

    }

    //用户登录
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User user1 = userService.login(user);
        System.out.println("user1 = " + user1);
        String token = jwtUtil.createJWT(user1.getId(), user.getNickname(), "user");
        System.out.println("token = " + token);
        Map map = new HashMap();
        map.put("token", token);
        map.put("name", user.getNickname());

        if (user1 != null) {
            return new Result(true, StatusCode.OK, "登录成功", map);
        }
        return new Result(true, StatusCode.OK, "登录失败");
    }

    //删除用户
    @DeleteMapping("{userId}")
    public Result deleteUser(@PathVariable String userId) {
        //判断此用户是否有权限...(拦截器中处理)
        //1.从request中取出admin_roles这个key对应的值
        Claims claims = (Claims) request.getAttribute("admin");
        System.out.println("claims = " + claims);
        if (claims != null) {
            userService.deleteUser(userId);
            return new Result(true, StatusCode.OK, "删除成功");
        }else {
            return new Result(false, StatusCode.OK, "删除失败");
        }
    }
    //增加关注数
    @PostMapping("/incfollow/{userid}/{x}")
    public void addfollow(@PathVariable String userid,@PathVariable int x){
        System.out.println("userid = " + userid);
        System.out.println("x = " + x);
        userService.addfollow(userid,x);
//        return new Result(true,StatusCode.OK,"关注成功");
    }
    //增加粉丝数
    @PostMapping("/incfans/{userid}/{x}")
    public void addfans(@PathVariable String userid,@PathVariable int x){
        System.out.println("userid111 = " + userid);
        System.out.println("x1111 = " + x);
        userService.addfans(userid,x);
//        return new Result(true,StatusCode.OK,"粉丝新增成功");
    }
}
